home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 03 Mar 1996 05:44:29 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar2224429@qcd.lanl.gov>
- References: <4gqpa1$3h9@alcor.usc.edu> <4hagqg$6je@daily-planet.execpc.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: sprecher@execpc.com's message of Sat, 02 Mar 1996 23:00:52 GMT
-
- In article <4hagqg$6je@daily-planet.execpc.com>
- sprecher@execpc.com (Reginald W. Sprecher) writes:
-
- <snip>
- RWS: 1. A pointer - a pointer is a variable which has as its purpose the
- RWS: ability to point to another variable. One thing that needs to be made
- RWS: clear here is that the size of a pointer is always the same no matter
- RWS: what type of data the pointer is pointing at ( a pointer to a char and
- RWS: an int are the same size). However, this is not and will not hold
-
- You should read the FAQ first: there is simply no point trying to
- `discover' the same incorrect statement over and over again. There is
- absolutely no requirement that pointers to different types will have
- the same size. There are many machines where this is not true. The FAQ
- gives examples.
-
- <snip>
- RWS: Now lets walk through a small program. (assume for this discussion
- RWS: that we are running on a 16 bit machine, aka pc-xt.
- RWS:
- RWS: void main(void)
-
- This statement leads to undefined behaviour. main must return int in C.
-
-
- RWS: a_pointer = c; // or a_pointer = &c[0]
-
- This is a syntax error in C.
-
- <snip>
- RWS: | | 1012 a_pointer (since we
- have assumed a
- RWS: | | 1013 16 bit machine a
- pointer lives in 2 bytes
-
- Not necessarily. First, a pointer may contain information in addition
- to the address. Second, on 16 bit word addressable machines with 8 bit
- chars, it might be useful to have longer pointers.
-
- <snip>
- RWS: 2. The next thing we encounter is the sizeof instructions. Be
- RWS: carefull here, sizeof looks like a C runtime function, acts like a C
- RWS: runtime function but is not a C runtime function. It is really some
-
- Which runtime function can be called on a type? Which can be called
- without parentheses? Remember sizeof (char*) and sizeof a are both
- valid.
-
- RWS: array_name[m][n]
- RWS:
- RWS: * ( *(array_name + (m * size_of_data_in_array) ) + (n *
- RWS: size_of_data_in_array) )
-
- Careful. I think, but am not sure, that you are confused. Have you
- read the FAQ about the difference between char **x and char x[5][3]
- for example?
-
- RWS: 1) Using the name of an array somehow yields or calculates or
- RWS: generates the address.
- RWS:
- RWS: This is not true. The name of the array is the address of the array.
- RWS: The operation is not a calculation or de-referencing, or ....
- RWS: I have personally never heard of this 'decay' process. If you look
- RWS: what is really happening inside the code there is no decay process.
- RWS: This is because the program always reference an array by its address
- RWS: which is the address of the first element in the array. This same
- RWS: pricincipal applies to multi-dimensional arrays as well.
-
- And precisely this is called the `decay'. In all type compatibility
- statements, an array name can be used and will have the same type as a
- pointer to the first element. Remember &array_name is also a pointer,
- but is not of the same type.
-
- RWS:
- RWS:
- RWS: 2) &c is a pointer to something.
- RWS:
- RWS: &c is by the above definition is not a pointer, it is an address, and
- RWS: specifically it is an address to a variable called c, which is of type
- RWS: char, and whose first element is at some address.
-
- The distinction between address and a pointer you are trying to make
- is exactly the difference between what in C is called a pointer value
- and a pointer lvalue. It is like the difference between an ordinary
- variable of type int (say i), and a value of type int, say 5. Calling
- &c not a pointer goes against all established definitions of what a
- pointer is.
-
- RWS:
- RWS: &c is the address of myarray which is:
- RWS:
- RWS: &c == c == &c[0] == 1008 (in our memory map)
-
- Any conforming compiler should diagnose the expression &c == c as
- erroneous (when c is an array): &c and c simply do not have the same
- type. c == &c[0] is true because of the `decay'.
-
- <snip>
- RWS: 3) Pointer take on different sizes.
- RWS:
- RWS: Pointers only take on a different size when going from one platform to
- RWS: another. If you write a program and define 1000 pointers that can
- RWS: point at base data types or your own structures, the size (amount of
- RWS: memory) that the pointer varialbe it self consumes is still the same
- RWS: size. Again, the factor here is what is the address space of your
- RWS: target system.
-
- This is simply incorrect: and shows that the poster does not have a
- good grasp of portable C; and is too arrogant to find the correct
- answers that are so easily available.
-
- RWS:
- RWS: Reginald W. Sprecher
- RWS: A.C.M.E. Consulting
- RWS: Assembler, C/C++,Microprocessor,Embedded Consulting
- RWS: sprecher@execpc.com
-
- I leave the signature in as I found it humorous at the end of such a
- completely erroneous post.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-